package org.gavin.sharding.test;

import java.util.concurrent.ThreadLocalRandom;

public class SimpleConfusion {

    public static void main(String[] args) {

        Integer random = Integer.parseInt(getRandomNum(1));

        String number = "901112229";

        String encrypt = encryptNumber(number, random);
        StringBuilder sb = new StringBuilder();
        sb.append(encrypt).append("180").append(getRandomNum(10)).append(random);
        System.out.println(encrypt + " " + sb.toString());


        String decrypt = decryptNumber(encrypt, random);
        System.out.println(decrypt);

        String value = "78999000718038702684248";
        String substring = value.substring(0, value.indexOf("180"));
        System.out.println(substring);
        String substring1 = value.substring(value.length() - 1);
        System.out.println(substring1);
        String decrypt1 = decryptNumber(substring, Integer.parseInt(substring1));
        System.out.println("======" + decrypt1);
    }

    private static String encryptNumber(String number, Integer random) {
        StringBuilder builder = new StringBuilder();
        char[] charArray = number.toCharArray();
        for (char c : charArray) {
            char bb = (char) (c + random);
            if (bb > 57 && bb < 67) {
                bb = (char) (bb - 10);
            }
            builder.append(bb);
        }
        return builder.toString();
    }

    private static String decryptNumber(String number, Integer random) {
        StringBuilder builder = new StringBuilder();
        char[] charArray = number.toCharArray();
        for (char c : charArray) {
            char bb = (char) (c - random);
            if (bb < 48 && bb > 38) {
                bb = (char) (bb + 10);
            }
            builder.append(bb);
        }
        return builder.toString();
    }

    private static String getRandomNum(int num) {
        StringBuilder numStr = new StringBuilder();
        while (true) {
            for (int i = 0; i < num; i++) {
                numStr.append(ThreadLocalRandom.current().nextInt(9));
            }
            if (numStr.indexOf("180") == -1) {
                return numStr.toString();
            }
            numStr.setLength(0);
        }
    }
}

